package com.foodplus.www.adapter.home;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.foodplus.www.bean.home.DataTypeBean;
import com.foodplus.www.bean.home.GoodsSpecialListBean;
import com.foodplus.www.foodplus.R;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import jp.wasabeef.glide.transformations.BlurTransformation;

/**
 * Created by Administrator on 2016/11/1 0001.
 */

public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.HomeViewHolder> {

    private static final int HOME_TYPE1 = 2;
    private static final int HOME_TYPE2 = 4;
    private static final int HOME_TYPE3 = 6;

    private List<DataTypeBean> data;
    private Context context;


    public HomeAdapter(List<DataTypeBean> data, Context context) {
        this.data = data;
        this.context = context;;
    }

    @Override
    public int getItemViewType(int position) {
        return Integer.valueOf(data.get(position).getRelation_object_type());
    }

    @Override
    public HomeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = null;
        HomeViewHolder holder = null;
        switch (viewType) {
            case HOME_TYPE1:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_layout1, parent, false);
                holder = new ViewHolder1(view);
                break;
            case HOME_TYPE2:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_layout2, parent, false);
                holder = new ViewHolder2(view);
                break;
            case HOME_TYPE3:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_layout3, parent, false);
                holder = new ViewHolder3(view);
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(HomeViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case HOME_TYPE1:
                final ViewHolder1 holder1 = (ViewHolder1) holder;
                Glide.with(context).load(data.get(position).getRelation_object_image())
                        .placeholder(R.mipmap.bg_morentu)
                        .error(R.mipmap.bg_morentu)
                        .fitCenter()
                        .into(holder1.homeImage1);
                holder1.homeTitle1.setText(data.get(position).getRelation_object_title());
                holder1.homeJingle1.setText(data.get(position).getRelation_object_jingle());
                holder1.homePrice1.setText("￥" + String.valueOf(data.get(position).getGoods_price()));
                holder1.homeCollection1.setText("收藏");
                break;
            case HOME_TYPE2:
                final ViewHolder2 holder2 = (ViewHolder2) holder;
                Glide.with(context).load(data.get(position).getRelation_object_image())
                        .bitmapTransform(new BlurTransformation(context))
                        .placeholder(R.mipmap.bg_morentu)
                        .error(R.mipmap.bg_morentu)
                        .into(holder2.homeImage2);

                holder2.homeTitle2.setText(data.get(position).getRelation_object_title());
                /**
                 * 横向recyclerview
                 */
                List<GoodsSpecialListBean> goods_special_list = data.get(position).getGoods_special_list();
                HomeChildAdapter homeChildAdapter = new HomeChildAdapter(goods_special_list,context);
                holder2.itemRecyclerView.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false));
                holder2.itemRecyclerView.setAdapter(homeChildAdapter);
                break;
            case HOME_TYPE3:
                final ViewHolder3 holder3 = (ViewHolder3) holder;
                Glide.with(context).load(data.get(position).getRelation_object_image())
                        .placeholder(R.mipmap.bg_morentu)
                        .error(R.mipmap.bg_morentu)
                        .fitCenter()
                        .into(holder3.homeImage3);
                break;
        }
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }


    public class HomeViewHolder extends RecyclerView.ViewHolder {
        public HomeViewHolder(View itemView) {
            super(itemView);
        }
    }

    public class ViewHolder1 extends HomeViewHolder {
        @BindView(R.id.home_image1)
        ImageView homeImage1;
        @BindView(R.id.home_title1)
        TextView homeTitle1;
        @BindView(R.id.home_jingle1)
        TextView homeJingle1;
        @BindView(R.id.home_price1)
        Button homePrice1;
        @BindView(R.id.home_collection1)
        Button homeCollection1;

        public ViewHolder1(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public class ViewHolder2 extends HomeViewHolder {
        @BindView(R.id.home_image2)
        ImageView homeImage2;
        @BindView(R.id.home_title2)
        TextView homeTitle2;
        @BindView(R.id.item_recycler)
        RecyclerView itemRecyclerView;

        public ViewHolder2(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public class ViewHolder3 extends HomeViewHolder {
        @BindView(R.id.home_image3)
        ImageView homeImage3;
        public ViewHolder3(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

}
